/* ========================================================================== */
/*   GameHandler.cpp                                                          */
/*   (c) 2012 Team 7 Hogeschool Utrecht ICT TI                                */
/*		Lars Stolwijk, Gerben Boot, Guido van Hienen, Marcel Boelen			  */
/*                                                                            */
/*   Responsible for handling the entire game. Reacts from incoming signals   */
/*		from OperationController, Health and GameTime.		 				  */
/* ========================================================================== */
#include "GameHandler.h"

GameHandler::GameHandler(int prio, SoundController * soundController, GameTime * gameTime, Health * health)
	: RTOS::task(prio, "GameHandlerTask"), 
	GameTaskTimer( this, "GameTaskTimer" ), 
	ZeroGameTimeFlag( this, "ZeroGameTimeFlag"), 
	DeathFlag( this, "DeathFlag"),
	StartGameFlag( this, "StartGameFlag"), 
	StopGameFlag( this, "StopGameFlag"), 
	theSoundController(soundController), 
	theGameTime(gameTime), 
	theHealth(health)
{
	introduceGame = new IntroduceGame(theSoundController);
	gameController = new GameController(theSoundController, theGameTime);
}

void GameHandler::main()
{
	CurrentState = WAITING;
	while(true){
		if(CurrentState == WAITING)
		{
			std::cout << "Waiting\n";
			RTOS::event e = wait( StartGameFlag );
			CurrentState = INTRODUCING;
		}
		else if(CurrentState == INTRODUCING)
		{
			std::cout << "Introducing\n";
			introduceGame->Start();
			theHealth->ResetHealth();
			while(true)
			{
				GameTaskTimer.set( 5 S );
				RTOS::event e = wait( );
				if(e == GameTaskTimer)
				{
					if(!introduceGame->Introduce())
					{
						CurrentState = RUNNING;
						break;
					}
				}
				else if(e == StopGameFlag)
				{
					GameTaskTimer.cancel();
					CurrentState = WAITING;
					break;
				}
			}
		}
		else if(CurrentState == RUNNING)
		{
			std::cout << "Running\n";
			gameController->Start();
			RTOS::event e = wait( );
			if(e == StopGameFlag)
			{
				CurrentState = WAITING;
				theGameTime->StartStopGameTime();
			}
			else if(e == DeathFlag)
			{
				CurrentState = BEINGDEATH;
				theGameTime->StartStopGameTime();
			}
			else if(e == ZeroGameTimeFlag)
			{
				CurrentState = FINISHING;
			}
		}
		else if(CurrentState == FINISHING){
		
			std::cout << "Times Up1\n";
			gameController->Finished();
			GameTaskTimer.set( 3 S);
			while(CurrentState == FINISHING)
			{
				RTOS::event e = wait( );
				if(e == StopGameFlag)
				{
					GameTaskTimer.cancel();
					CurrentState = WAITING;
					break;
				}
				else if(e == GameTaskTimer)
				{
					gameController->Finished();
					GameTaskTimer.set( 3 S);
				}
			}
		}
		else if(CurrentState == BEINGDEATH)
		{
			std::cout << "Death\n";
			gameController->Death();
			GameTaskTimer.set( 3 S);
			while(CurrentState == BEINGDEATH)
			{
				RTOS::event e = wait( );
				if(e == StopGameFlag)
				{
					GameTaskTimer.cancel();
					CurrentState = WAITING;
					break;
				}
				else if(e == GameTaskTimer)
				{
					gameController->Death();
					GameTaskTimer.set( 3 S);
				}
			}
		}
	}
}

void GameHandler::ZeroGameTime()
{
	ZeroGameTimeFlag.set();
}

void GameHandler::Death()
{
	DeathFlag.set();
}

void GameHandler::StartGame()
{
	StartGameFlag.set();
}

void GameHandler::StopGame()
{
	StopGameFlag.set();
}
